//
//  EssenceController.m
//  Budejie
//
//  Created by 远成物流 on 2017/5/15.
//  Copyright © 2017年 陈梦佳. All rights reserved.
//

#import "EssenceController.h"
#import "UIBarButtonItem+Item.h"
#import "VoiceController.h"
#import "AllController.h"
#import "VideoController.h"
#import "PictureController.h"
#import "WordController.h"

@interface EssenceController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIView *titleView;

@property(nonatomic,strong)UIView *titleUnderline;

@property(nonatomic,strong)UIButton *previousClickedTitleButton;

@property(nonatomic,strong)UIScrollView *scrollView;

@end

@implementation EssenceController

- (void)viewDidLoad {
    [super viewDidLoad];
    //让导航栏不透明且占空间位置，所以我们的坐标就会从导航栏下面开始算起。
    self.navigationController.navigationBar.translucent = NO;
    //解决scrollview中的view下移64个像素的问题
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航条
    [self setupNavBar];
    
    //设置子控制器
    [self setupChildController];
    
    //添加scrollview
    [self setupScrollView];
    
    //设置标题栏
    [self setupTitlesView];
    
    // 添加第0个子控制器的view
    [self addChildVcViewIntoScrollView:0];
}

-(void)setupNavBar{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"nav_item_game_icon"] highImage:[UIImage imageNamed:@"nav_item_game_click_icon"] target:self action:@selector(leftBarButtonClicked:)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationButtonRandom"] highImage:[UIImage imageNamed:@"navigationButtonRandomClick"] target:self action:@selector(rightBarButtonClicked:)];
    
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
}

-(void)setupTitlesView{
    UIView *titleView = Alloc(UIView);
    titleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    titleView.frame = CGRectMake(0, 0, ScreenW, TitleViewHeight);
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //设置标题栏按钮
    [self setupTitleButton];
    
    //设置标题栏下划线
    [self setupTitleUnderline];
}

-(void)setupTitleButton{
    // 文字
    NSArray *titles = @[@"全部",@"声音", @"视频", @"图片", @"段子"];
    NSUInteger count = titles.count;
    
    CGFloat w = self.titleView.width_sd / count;
    CGFloat h = self.titleView.height_sd;
    
    for (int i=0; i<count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button addTarget:self action:@selector(titleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleView addSubview:button];
        
        button.frame = CGRectMake(i * w, 0, w, h);
    }
}

-(void)setupTitleUnderline{
    UIButton *firstBtn = self.titleView.subviews.firstObject;
    
    UIView *underLine = Alloc(UIView);
    underLine.height_sd = 2;
    underLine.top_sd = self.titleView.height_sd - underLine.height_sd;
    //根据文字内容自适应label尺寸
    [[firstBtn titleLabel] sizeToFit];
    underLine.width_sd = firstBtn.titleLabel.width_sd + B_Margin;
    underLine.centerX_sd = firstBtn.centerX_sd;
    [self.titleView addSubview:underLine];
    
    self.titleUnderline = underLine;
    
    //切换按钮状态
    firstBtn.selected = YES;
    self.previousClickedTitleButton = firstBtn;
    
    underLine.backgroundColor = [firstBtn titleColorForState:UIControlStateSelected];
}

-(void)setupChildController{
    [self addChildViewController:[[AllController alloc] init]];
    [self addChildViewController:[[VoiceController alloc] init]];
    [self addChildViewController:[[VideoController alloc] init]];
    [self addChildViewController:[[PictureController alloc] init]];
    [self addChildViewController:[[WordController alloc] init]];
}

-(void)setupScrollView{
    UIScrollView *scrollView = Alloc(UIScrollView);
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.frame = self.view.bounds;
    scrollView.scrollsToTop = NO; // 点击状态栏的时候，这个scrollView不会滚动到最顶部
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor orangeColor];
    
    NSUInteger count = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(count * scrollView.width_sd, 0);
    
    self.scrollView = scrollView;
}

#pragma mark - 按钮点击
-(void)leftBarButtonClicked:(id)sender{
    
}

-(void)rightBarButtonClicked:(id)sender{
    
}

//标题栏按钮点击
-(void)titleButtonAction:(UIButton *)sender{
    //重复点击
    if (self.previousClickedTitleButton == sender) {
        [[NSNotificationCenter defaultCenter] postNotificationName:TitleButtonDidRepeatClickedNotification object:nil];
    }
    //处理按钮点击
    [self dealTitleButtonClick:sender];
}

-(void)dealTitleButtonClick:(UIButton *)sender{
    //切换按钮状态
    self.previousClickedTitleButton.selected = NO;
    sender.selected = YES;
    self.previousClickedTitleButton = sender;
    
    NSUInteger index = sender.tag;
    [UIView animateWithDuration:0.25 animations:^{
        //下划线
        self.titleUnderline.width_sd = sender.titleLabel.width_sd + B_Margin;
        self.titleUnderline.centerX_sd = sender.centerX_sd;
        
        //scrollview
        self.scrollView.contentOffset = CGPointMake(index * self.scrollView.width_sd, self.scrollView.contentOffset.y);
    } completion:^(BOOL finished) {
        // 添加子控制器的view
        [self addChildVcViewIntoScrollView:index];
    }];
    
    // 设置index位置对应的tableView.scrollsToTop = YES， 其他都设置为NO
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController *childVC = self.childViewControllers[i];
        
        // 如果view还没有被创建，就不用去处理
        if (![childVC isViewLoaded]) {
            continue;
        }
        
        UIScrollView *scrollView = (UIScrollView *)childVC.view;
        
        if (![scrollView isKindOfClass:[UIScrollView class]]) {
            continue;
        }
        
        scrollView.scrollsToTop = (i==index);
    }
}

-(void)addChildVcViewIntoScrollView:(NSInteger)index{
    UIViewController *viewController = self.childViewControllers[index];
    
    // 如果view已经被加载过，就直接返回
    if ([viewController isViewLoaded]) {
        return;
    }
    
    // 取出index位置对应的子控制器view
    UIView *childView = viewController.view;
    
    // 设置子控制器view的frame
    childView.frame = CGRectMake(index * self.scrollView.width_sd, 0, self.scrollView.width_sd, self.scrollView.height_sd);
    
    // 添加子控制器的view到scrollView中
    [self.scrollView addSubview:childView];
}

#pragma mark - UIScrollView delegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 求出标题按钮的索引
    NSInteger index = scrollView.contentOffset.x / self.scrollView.width_sd;
    
    // 点击对应的标题按钮
    UIButton *button = self.titleView.subviews[index];
    [self dealTitleButtonClick:button];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
